package com.platform.study.thread;

/**
 * 用实现Runnable接口方式实现线程:对象共享
 * User: wangyanginfo
 * Date: 12-11-26
 * Time: 下午2:44
 */
public class RunnableSimple_1 implements Runnable {

    private int ticket = 500;  //500张票

    private int[] threadArray = new int[]{0, 0};

    public int[] getThreadArray() {
        return threadArray;
    }

    public void setThreadArray(int[] threadArray) {
        this.threadArray = threadArray;
    }

    @Override
    public void run() {
        while (ticket > 0) {
            if (Thread.currentThread().getName().equals("线程1")) {
                threadArray[0]++;
            } else if (Thread.currentThread().getName().equals("线程2")) {
                threadArray[1]++;
            }
            System.out.println(Thread.currentThread().getName()
                    + " 运行 _  " + ticket--);
        }
    }

    public static void main(String[] args) {
        RunnableSimple_1 simpleA = new RunnableSimple_1();
        Thread thread = new Thread(simpleA, "线程1");
        System.out.println("线程启动之前---》" + thread.isAlive());
        thread.start();
        System.out.println("线程启动之后---》" + thread.isAlive());
        Thread thread1 = new Thread(simpleA, "线程2");
        System.out.println("线程启动之前---》" + thread1.isAlive());
        thread1.start();
        System.out.println("线程启动之后---》" + thread1.isAlive());

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程1处理个数：" + simpleA.getThreadArray()[0]);
        System.out.println("线程2处理个数：" + simpleA.getThreadArray()[1]);
    }
}
